// pages/batchOrder/batchOrder.js
const app = getApp()
const util = require("../../utils/util");

Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {},
    currentFirstDate: '',
    weekArr: [],
    checkboxGroup: [],
    config: {
      content: [],
      newContent: [],
      titleContent: [],
      titles: ['周', '日期/餐别', '早餐', '午餐', '晚餐'],
      props: ['id', 'name', 'age', 'school', 'name'],
      columnWidths: ['80rpx', '140rpx', '120rpx', '120rpx', '120rpx'],
      border: true,
      stripe: true,
      // headbgcolor : '#dddddd'
    },
    tableInfo: {},
    items: [{
      name: 'C1W1',
      order_type: '1',
      date: '2020-07-07',
      week: '周一',
      checked: true,
    }]

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // console.log(this.setDate(this.addDate(this.data.currentFirstDate),-7))
    console.log(this.getWeekArr(new Date(), 0))


    this.setData({
      userInfo: wx.getStorageSync('userInfo'),
      weekArr: this.getWeekArr(new Date(), 0)
    })
    console.log(this.data.weekArr)
    this.genContent(this.data.weekArr)

    let titleContent = [{
        name: '周',
        value: "0"
      },
      {
        name: '日期/餐别',
        value: "1"
      }, {
        name: '早餐',
        value: "2",
        checked: 'false'
      }, {
        name: '午餐',
        value: "3",
        checked: 'false'
      }, {
        name: '晚餐',
        value: "4",
        checked: 'false'
      },
    ]
    let content = [{
        name: '一',
        value: 'c0',
        pvalue: '0' //星期几
      },
      {
        name: '2020-07-07',
        value: 'c1',
        checked: 'false',
        pvalue: '1' //日期
      },
      {
        name: '',
        value: 'c2',
        checked: 'false',
        pvalue: '2' //早餐
      },
      {
        name: '',
        value: 'c3',
        checked: 'false',
        pvalue: '3' //午餐
      },
      {
        name: '',
        value: 'c3',
        checked: 'false',
        pvalue: '3' //午餐
      },
      {
        id: 6,
        date: '2020-07-10',
        breakfast: true,
        lunch: true,
        dinner: false
      },
      {
        id: 7,
        date: '2020-07-11',
        breakfast: true,
        lunch: true,
        dinner: false
      }

    ]
    // let that = this
    // 此处模拟网络请求
    // setTimeout(function () {
    //   that.setData({
    //     'config.content': newContent
    //   })
    // }, 1000)
  },

  // 生成数组数据
  genContent(weekArr,room){
    let newContent = []
    for (let j = 1; j <= 7; j++) {
      let tmp = []
      for (let i = 1; i <= 3; i++) {
        const id = 'C' + i + 'W' + j;
        tmp.push({
          id: id,
          order_type: i,
          order_count: 1,
          extra_order_date: weekArr[j - 1].date,
          week:weekArr[j - 1].week.replace('周', ''),
          person: this.data.userInfo.name,
          number: this.data.userInfo.number,
          room: room? room: '西南油田食堂',
          quantity: 1,
          type: 1,
          time: util.formatTime(new Date()),
          checked: true,
          disabled: false
        }, )
      }
      newContent.push(tmp)
    }
    console.log(newContent)
    this.setData({
      'config.content': newContent
    })
  },
  onChange(event) {
    this.setData({
      radio: event.detail,
    });
  },
  //添加订餐记录
  addOrder(e) {
    console.log('添加订餐记录', e)
    let list = this.data.newContent;
    for (let i = 0; i < list.length; i++) {
      console.log(list)
      
    }


  },
  //删除订餐记录
  delOrder(e) {
    console.log('删除订餐记录', e)
  },
  checkboxChange(e) {
    console.log(e)
    console.log('checkbox发生change事件，携带value值为：', e.detail.value)
    this.setData({
      checkboxGroup: e.detail.value
    })
  },
  //上周
  chooseLastWeek() {
    this.setData({
      weekArr: this.getWeekArr(new Date(), -7)
    })
    console.log(this.data.weekArr)
    this.genContent(this.data.weekArr)
  },
  chooseThisWeek() {
    this.setData({
      weekArr: this.getWeekArr(new Date(), 0)
    })
    console.log(this.data.weekArr)
    this.genContent(this.data.weekArr)
  },
  chooseNextWeek() {
    this.setData({
      weekArr: this.getWeekArr(new Date(), 7)
    })
    console.log(this.data.weekArr)
    this.genContent(this.data.weekArr)
  },

  //全选
  checkAllTrue(e) {
    console.log(e)
    console.log('全选')
    const that = this;
    let list = that.data.config.content
    for (let i = 0; i < list.length; i++) {
      for (let j = 0; j < list[i].length; j++) {
        list[i][j].checked = !list[i][j].checked || false;
      }
    }
    this.setData({
      'config.content': list
    })
  },
  //全清
  checkAllFalse(e) {
    console.log(e)
    console.log('全清')
    const that = this;
    let list = that.data.config.content
    for (let i = 0; i < list.length; i++) {
      for (let j = 0; j < list[i].length; j++) {
        list[i][j].checked = !list[i][j].checked || false;
      }
    }
    this.setData({
      'config.content': list
    })
  },

  getWeekArr(date, n) {
    switch (n) {
      case 0:
        return this.setDate(date)
      case -7:
        return this.setDate(this.addDate(this.data.currentFirstDate, -7))
      case 7:
        return this.setDate(this.addDate(this.data.currentFirstDate, 7))
      default:
        return
    }
  },

  setDate(date) {
    var week = date.getDay() - 1;
    date = this.addDate(date, week * -1);
    this.setData({
      currentFirstDate: new Date(date)
    })

    const arr = []
    for (let i = 0; i < 7; i++) {
      const d = util.formatDate(i == 0 ? date : this.addDate(date, 1))
      arr.push(d);
    }

    return arr;
  },

  addDate(date, n) {
    date.setDate(date.getDate() + n);
    return date;
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {


  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})